package com.example.demo.controller;

import com.example.demo.util.MapMesg;
import com.example.demo.util.ResMesg;
import com.example.demo.util.TableMesg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by lsp on 16/7/29.
 */
public class BaseController {
    private static Logger logger = LoggerFactory.getLogger(BaseController.class);
    @Autowired
    protected Environment env;

    protected ResMesg resMesg = new ResMesg();

    public static final String POI_LOGIN_USER = "POI_LOGIN_USER";

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public ResMesg handleException(Throwable t) {
        logger.error("Unexpected throwable:     ", t);
        resMesg = new ResMesg();
        resMesg.setStatus(500);
        resMesg.setMsg("Error");

        return resMesg;
    }

    protected void checkOptionCondition(Map<String, Object> paramMap, String name, String noUseValue) {
        String s = (String) paramMap.get(name);
        if (s != null && s.equals(noUseValue)) {
            paramMap.remove(name);
        }
    }


    protected ResMesg sendTABLEJSON(long total, Object rows) {
        try {
            TableMesg tableMesg = new TableMesg();
            tableMesg.setTotal(total);
            tableMesg.setRows(rows);

//            logger.debug(tableMesg.toString());
            return tableMesg;
        } catch (Exception e) {
            logger.error("send sendTABLEJSON error", e);
        }
        return null;
    }

    //zy modify add status
    protected MapMesg sendMapJson(Map<String, Object> paramMap) {
        MapMesg mapMesg = new MapMesg();
        mapMesg.setData(paramMap);
        return mapMesg;
    }

    protected ResMesg sendObj(Object obj) {
        resMesg = new ResMesg();
        resMesg.setStatus(0);
        resMesg.setMsg("");
        resMesg.setData(obj);
        return resMesg;
    }

    protected ResMesg sendSuccess() {
        resMesg = new ResMesg();
        resMesg.setStatus(0);
        resMesg.setMsg("");
        return resMesg;
    }
    protected ResMesg sendSuccess(String msg) {
        resMesg = new ResMesg();
        resMesg.setStatus(0);
        resMesg.setMsg(msg);
        return resMesg;
    }

    protected ResMesg sendFailed() {
        resMesg = new ResMesg();
        resMesg.setStatus(1);
        resMesg.setMsg("");
        return resMesg;
    }

    protected ResMesg sendErrorMessage(String mes) {
        resMesg = new ResMesg();
        resMesg.setStatus(-1);
        resMesg.setMsg(mes);
        return resMesg;
    }


    protected ResMesg sendCount(int count) {
        resMesg = new ResMesg();
        resMesg.setMsg("");
        resMesg.setStatus(count);
        return resMesg;
    }

    public String getUUID() {
        return UUID.randomUUID().toString();
    }

    public PageRequest getPageRequest(int page, int pageSize, String ascOrDesc, String... sortName) {
        //logger.info("getPageRequest   page : "+page+" pageSize : "+pageSize+" ascOrDesc : "+ascOrDesc+" sortName : "+sortName);
        if (page == 0) {
            page = 1;
            pageSize = 10;
        }

        Sort.Direction order = Sort.Direction.ASC;
        if (!"asc".equals(ascOrDesc)) {
            order = Sort.Direction.DESC;
        }
        page = page <= 0 ? 0 : (page - 1);
        return new PageRequest(page, pageSize, order, sortName);
    }
}
